package jp.gnt.swf;

import sun.security.util.BitArray;

public class RECT {

	public int nBits = 0;
	public int Xmin = 0; // in twip
	public int Xmax = 0; // in twip
	public int Ymin = 0; // in twip
	public int Ymax = 0; // in twip

	public RECT(int nBits, int Xmin, int Xmax, int Ymin, int Ymax) {
		this.nBits = nBits;
		this.Xmin = Xmin;
		this.Xmax = Xmax;
		this.Ymin = Ymin;
		this.Ymax = Ymax;
	}

	/**
	 * @return width-size in pixel
	 */
	public int getWidth() {
		return (Xmax - Xmin) / 20;
	}

	/**
	 * @return height-size in pixel
	 */
	public int getHeight() {
		return (Ymax - Ymin) / 20;
	}

	/**
	 * @return RECT in byte[]
	 */
	public byte[] toByteArray() {
		int bitOffset = 0;

		int totalBit = 5 + (4 * nBits);
		BitArray bitArray = new BitArray(totalBit);

		for (int i = 4; i >= 0; i--) {
			bitArray.set(bitOffset, ((nBits >> i) & 0x01) == 0x01);
			bitOffset++;
		}

		int xmin = Xmin << (32 - nBits);

		for (int i = 0; i < nBits; i++) {
			bitArray.set(bitOffset, ((xmin << i) & 0x80000000) == 0x80000000);
			bitOffset++;
		}

		int xmax = Xmax << (32 - nBits);

		for (int i = 0; i < nBits; i++) {
			bitArray.set(bitOffset, ((xmax << i) & 0x80000000) == 0x80000000);
			bitOffset++;
		}

		int ymin = Ymin << (32 - nBits);

		for (int i = 0; i < nBits; i++) {
			bitArray.set(bitOffset, ((ymin << i) & 0x80000000) == 0x80000000);
			bitOffset++;
		}

		int ymax = Ymax << (32 - nBits);

		for (int i = 0; i < nBits; i++) {
			bitArray.set(bitOffset, ((ymax << i) & 0x80000000) == 0x80000000);
			bitOffset++;
		}

		return bitArray.toByteArray();
	}
}